package com.lovo.sgproj.frame.studentmanage;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JDialog;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoLabel;
import com.lovo.sgproj.Config;
import com.lovo.sgproj.bean.RoomBean;
import com.lovo.sgproj.bean.StudentBean;
import com.lovo.sgproj.frame.MainFrame;
import com.lovo.sgproj.service.RoomService;
import com.lovo.sgproj.service.StudentService;
import com.lovo.sgproj.service.imp.RoomServiceImp;
import com.lovo.sgproj.service.imp.StudentServiceImp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class StudentChgDialog extends JDialog {
    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

    private LovoLabel nameLab;
    private LovoLabel genderLab;
    private LovoLabel classLab;
    private LovoLabel oldRoomLab;
    private LovoComboBox<RoomBean> newRoomCmb;//后期换做房间Bean

    private LovoButton chgBtn;
    private LovoButton cancelBtn;

    private MainFrame frame;
    private int stuid;

    public StudentChgDialog(MainFrame frame, int stuid) {
        super(frame, "学生换房", true);

        this.frame = frame;
        this.stuid = stuid;

        this.setBounds(450, 350, 450, 280);
        this.setLayout(null);
        this.init();

        this.setVisible(true);
    }

    private void init() {
        // TODO Auto-generated method stub
        StudentService stuService = context.getBean(StudentServiceImp.class);
        StudentBean student = stuService.showStudent(stuid);
        System.out.println(student);

        String stuname = student.getStudentname();
        Boolean flag = student.getGender();
        String gender = "男";
        if (!flag) {
            gender = "女";
        }
        String stuclass = student.getTheClass().getClassName();
        String sturoom = student.getTheRoom().getAddress();

        this.nameLab = new LovoLabel("姓名", stuname, 20, 20, this);
        this.genderLab = new LovoLabel("性别", gender, 220, 20, this);
        this.classLab = new LovoLabel("班级", stuclass, 20, 80, this);
        this.oldRoomLab = new LovoLabel("原房间", sturoom, 220, 80, this);

        ArrayList<RoomBean> roomLst = new ArrayList<>();
        RoomService roomService = context.getBean(RoomServiceImp.class);
        roomLst = roomService.showChangableRoom(student);
        this.newRoomCmb = new LovoComboBox<RoomBean>("房间", roomLst, 20, 140, this);
        newRoomCmb.setList(roomLst);

        this.chgBtn = new LovoButton("确定", 120, 200, this);
        this.cancelBtn = new LovoButton("取消", 270, 200, this);
        this.chgBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                StudentService studentService = context.getBean(StudentServiceImp.class);
                int roomid = newRoomCmb.getItem().getRoomID();
                studentService.changeStudentRoom(stuid, roomid);
                //System.out.println(roomid);
                frame.getStudentNode().addListener();

                StudentChgDialog.this.dispose();
            }
        });

        this.cancelBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                StudentChgDialog.this.dispose();
            }
        });

    }
}
